package com.hhp.hhptt.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhp.hhptt.common.Constants;
import com.hhp.hhptt.common.Result;
import com.hhp.hhptt.controller.dto.UserDTO;
import com.hhp.hhptt.entity.User;
import com.hhp.hhptt.mapper.UserMapper;
import com.hhp.hhptt.service.UserService;
import com.hhp.hhptt.utils.TokenUtils;
import com.mysql.cj.util.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;


@RestController
@RequestMapping("/user")
public class UserController {
    private static Logger logger  = LoggerFactory.getLogger(UserController.class);
//    @Autowired
//    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    @Autowired
    private TokenUtils tokenUtils;

//    @Autowired
//    private


    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO){
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if(StringUtils.isNullOrEmpty(username) || StringUtils.isNullOrEmpty(password)){
            return Result.error(Constants.CODE_400,"参数错误");
        }
        UserDTO dto = userService.login(userDTO);
        return Result.success(dto);
    }


    @GetMapping("/index")
    public String index(){
        User user = new User();

        return "hello this is a hello world!!!";
    }

    @GetMapping("/getAll")
    public List getAll(){
//        List<User> lists = userMapper.findall();
        List<User> lists =userService.list();
        return lists;
    }

    //查询所有用户
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "") String username,
                        @RequestParam(defaultValue = "") String address,
                        @RequestParam(defaultValue = "1") Integer pageNum,
                        @RequestParam(defaultValue = "10") Integer pageSize) {
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();

        if (StrUtil.isNotBlank(username)) {
            objectQueryWrapper.like("username", username);
        }
        if (StrUtil.isNotBlank(address)) {
            objectQueryWrapper.like("address", address);
        }
        IPage<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userIPage = userService.page(page, objectQueryWrapper);
        return Result.success(userIPage);
    }



    @GetMapping("/info")
    public Result info(){
        User currentUser = tokenUtils.getCurrentUser();
        return Result.success(currentUser);
    }


    //返回指定用户信息

    @PostMapping("/save")
    public Boolean save(@RequestBody  User user){

        return userService.saveUser(user);
    }

    @PostMapping("/register")
    public Result register(@RequestBody  UserDTO userDTO){
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();

        if(StrUtil.isBlank(username) || StrUtil.isBlank(password)){
            return Result.error(Constants.CODE_400,"参数出错误！");
        }
        return Result.success(userService.register(userDTO));
    }



//    @DeleteMapping("/delete/{id}")
//    public Boolean delete(@PathVariable Integer id){
//      return userService.removeById(id);
//    }

    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable  Integer id){
        return userService.removeById(id);
    }

    @PostMapping("/deleteBatch")
    public Boolean deleteBatch(@RequestBody  List<Integer> ids){

        return userService.removeBatchByIds(ids);
    }

    @GetMapping("/username/{username}")
    public Result findByName(@PathVariable  String username){
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("username",username);

        return Result.success(userService.getOne(objectQueryWrapper));
    }
    @GetMapping("/page")
//    public Map<String,Object>  findPage(@RequestParam Integer pageNum,
         public IPage<User>  findPage(@RequestParam Integer pageNum,
                                      @RequestParam Integer pageSize,
                                      @RequestParam(defaultValue = "") String userName,
                                      @RequestParam(defaultValue = "") String email,
                                      @RequestParam(defaultValue = "") String address){
//       pageNum  = (pageNum-1)*pageSize;
//       List<User> data = userMapper.selectPage(pageNum,pageSize,userName);
//       Integer total = userMapper.selectTotal(userName);
//       Map<String,Object> result = new HashMap<>();
//       result.put("data",data);
//       result.put("total",total);
//       return result;

       IPage<User> page = new Page<>(pageNum,pageSize);
        QueryWrapper<User>  queryWrapper = new QueryWrapper<>();

        if(!"".equals(userName)){
            queryWrapper.like("userName",userName);
        }
        if(!"".equals(email)){
            queryWrapper.like("email",email);
        }

        if(!"".equals(address)) {
            queryWrapper.like("address", address);
        }

        queryWrapper.orderByAsc("id");
        User currentUser = tokenUtils.getCurrentUser();

        logger.info("currentUser username=="+currentUser.getUsername());
        return userService.page(page,queryWrapper);



    }



}
